package com.kejiguan.ui.presenter;

import android.widget.Toast;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.model.Shedule;
import com.kejiguan.ui.model.UserManagerModer;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.AddSheduleResponse;
import com.kejiguan.ui.response.AddUserResponse;
import com.kejiguan.ui.response.DelSheduleResponse;
import com.kejiguan.ui.response.ModifySheduleResponse;
import com.kejiguan.ui.response.QuerySheduleResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 2016/5/12.
 */
public class SheduleFragmentPresenter extends BaseFragmentPresenter{


    public void addShedule(Shedule shedule)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","712");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        params.put("operType", "add");
        params.put("scheduleName",shedule.getScheduleName());
        params.put("scheduleType", shedule.getScheduleType());
        params.put("scheduleTime", shedule.getScheduleTime());
        params.put("scheduleAction",shedule.getScheduleAction());
        params.put("scheduleObject",shedule.getScheduleObject());
        params.put("scheduleState",shedule.getScheduleState());
        params.put("scheduleNote", shedule.getScheduleNote());

        final GsonRequest<AddSheduleResponse> request = new GsonRequest("addShedule", AddSheduleResponse.class, new Response.Listener<AddSheduleResponse>() {
            @Override
            public void onResponse(AddSheduleResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "新增定时任务失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void modifyShedule(Shedule shedule)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","712");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        params.put("operType", "mod");
        params.put("scheduleId", String.valueOf(shedule.getScheduleId()));
        params.put("scheduleNo",String.valueOf(shedule.getScheduleNo()));
        params.put("scheduleName",shedule.getScheduleName());
        params.put("scheduleType", shedule.getScheduleType());
        params.put("scheduleTime", shedule.getScheduleTime());
        params.put("scheduleAction",shedule.getScheduleAction());
        params.put("scheduleObject",shedule.getScheduleObject());
        params.put("scheduleState",shedule.getScheduleState());
        params.put("scheduleNote", shedule.getScheduleNote());

        final GsonRequest<ModifySheduleResponse> request = new GsonRequest("modifyShedule", ModifySheduleResponse.class, new Response.Listener<ModifySheduleResponse>() {
            @Override
            public void onResponse(ModifySheduleResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "修改定时任务失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void queryShedules()
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","712");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        params.put("operType", "queryList");

        final GsonRequest<QuerySheduleResponse> request = new GsonRequest("querySheduleList", QuerySheduleResponse.class, new Response.Listener<QuerySheduleResponse>() {
            @Override
            public void onResponse(QuerySheduleResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "新增定时任务失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void delShedule(Shedule shedule)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","712");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        params.put("operType", "delete");
        params.put("scheduleId", String.valueOf(shedule.getScheduleId()));

        final GsonRequest<DelSheduleResponse> request = new GsonRequest("delShedule", DelSheduleResponse.class, new Response.Listener<DelSheduleResponse>() {
            @Override
            public void onResponse(DelSheduleResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "新增定时任务失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);


    }


}
